﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.RoleHandler
{
    public class CreateRoleUserHandler : IRequestHandler<CreateRoleUserCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserRole> urRep;
        private readonly IMapper mapper;
         
        public CreateRoleUserHandler(IBaseRepository<UserRole> urRep,IMapper mapper)
        {
            this.urRep = urRep;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(CreateRoleUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            foreach (var item in request.UserIds)
            {
                var ur = new UserRole();
                ur.UserId = item;
                ur.RoleId = request.RoleId;
                urRep.Add(ur);
            }


            result.Code = APIEnum.Success;
            result.Msg = "添加角色用户关系表成功";

            return await Task.FromResult(result);
        }
    }
}
